package com.hontech.bread.ui.parts

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.FrameLayout
import com.hontech.bread.R
import com.hontech.bread.device.leftRotationClose
import com.hontech.bread.device.leftRotationRow
import com.hontech.bread.device.rightRotationClose
import com.hontech.bread.device.rightRotationRow
import com.hontech.bread.ui.dialog.runProg
import com.hontech.bread.ui.view.LabelSpinner
import com.hontech.bread.utils.onClick
import com.hontech.bread.utils.stringById

class RotationMotorParts(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_rotation_motor, this)

        val leftSp = view.findViewById<LabelSpinner>(R.id.id_rotation_ls_left)
        val rightSp = view.findViewById<LabelSpinner>(R.id.id_rotation_ls_right)

        val leftBtn = view.findViewById<Button>(R.id.id_rotation_left_btn_run)
        val rightBtn = view.findViewById<Button>(R.id.id_rotation_right_btn_run)

        leftBtn.onClick {
            runProg(
                stringById(R.string.t1733460848_437),
                "${stringById(R.string.t1733460848_438)}:${leftSp.selectText()}${stringById(R.string.t1733460848_439)}"
            ) {
                try {
                    leftRotationRow(leftSp.selectText().toInt())
                } finally {
                    leftRotationClose()
                }
            }
        }
        rightBtn.onClick {
            runProg(
                stringById(R.string.t1733460848_440),
                "${stringById(R.string.t1733460848_441)}:${rightSp.selectText()}${stringById(R.string.t1733460848_442)}"
            ) {
                try {
                    rightRotationRow(rightSp.selectText().toInt())
                } finally {
                    rightRotationClose()
                }
            }
        }
    }
}

